package top.so.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import top.so.dto.ColumnDTO;
import top.so.dto.UserDTO;

import top.so.service.ColumnService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/column")
@SessionAttributes("isSuccess")//操作状态
public class BlogColumnController {

    @Autowired
    private ColumnService columnService;

    @RequestMapping()
    public ModelAndView column(ColumnDTO columnDTO){
        List<ColumnDTO> columnDTOList = columnService.selectWithPage(columnDTO,1);
        ModelAndView mv = new ModelAndView("main/BlogColumn");
        //保存当前用户所选的专栏类型
        if(columnDTO.getColumnType()!=null&&columnDTO.getColumnType().length()>0){
            mv.addObject("columnType",columnDTO.getColumnType());
        }
        if(columnDTOList!=null&&!columnDTOList.isEmpty()){
            mv.addObject("columnDTOList",columnDTOList);
        }
        return mv;
    }


    @RequestMapping(value = "newColumn",method = RequestMethod.POST)
    public ModelAndView newColumn(ColumnDTO columnDTO, HttpSession httpSession){
        ModelAndView mv = new ModelAndView("redirect:/myBlog/column");
        UserDTO userDTO = (UserDTO) httpSession.getAttribute("currUser");
        columnDTO.setUserID(userDTO.getUserID());
        if(columnService.insertColumn(columnDTO)){
            httpSession.setAttribute("isSuccess","true");
        }
        else{
            httpSession.setAttribute("isSuccess","false");
        }
        return mv;
    }



    @RequestMapping(value = "goPage",method = RequestMethod.POST)
    public ModelAndView goPage(int pageNum,HttpSession httpSession){
        UserDTO userDTO = (UserDTO) httpSession.getAttribute("currUser");
        ModelAndView mv = new ModelAndView("user/Column");
        ColumnDTO columnDTO = new ColumnDTO();
        columnDTO.setUserID(userDTO.getUserID());
        List<ColumnDTO> columnDTOList = columnService.selectWithPage(columnDTO,pageNum);
        mv.addObject("columnDTOList",columnDTOList);
        return mv;
    }

    @RequestMapping(value = "loadMoreColumnList",method = RequestMethod.GET)
    public ModelAndView loadMoreColumnList(ColumnDTO columnDTO,int pageNum){
        ModelAndView mv = new ModelAndView("main/BlogColumn");
        List<ColumnDTO> columnDTOList=columnService.selectWithPage(columnDTO,pageNum);
        if(columnDTOList!=null&&!columnDTOList.isEmpty()){
            mv.addObject("columnDTOList",columnDTOList);
        }
        return mv;
    }

    @RequestMapping(value = "deleteColumn",method = RequestMethod.POST)
    public ModelAndView deleteColumn(ColumnDTO columnDTO,ModelMap modelMap){
        ModelAndView mv = new ModelAndView("redirect:/myBlog/column");
        if(columnService.deleteColumn(columnDTO)){
            modelMap.addAttribute("isSuccess","true");
        }
        else{
            modelMap.addAttribute("isSuccess","false");
        }
        return mv;
    }

}
